package message

import (
	"common/models"
	"common/validate"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"project/docModel"
)

func (h MHandler) AddMessageToUser(c *gin.Context) {
	resp := new(models.RespData)
	req := new(docModel.AddMessageToUserReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("User:AddMessageToUser with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("User:AddMessageToUser with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//查看是否这条消息已经被创建
	message := &models.Message{
		From:   req.From,
		To:     req.To,
		Type:   req.Type,
		Body:   req.Body,
		ToName: req.ToNickName,
		Status: "请求中",
	}
	check, _, err := h.M.MessageCheck(message)
	if err != nil {
		zap.L().Error("Message AddMessageToUser h.M.MessageCheck", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if check {
		resp.SendCode(c, models.CodeUserExist)
		return
	}
	//保存
	if err := h.M.MessageAdd(message); err != nil {
		zap.L().Error("Message AddMessageToUser h.M.MessageAdd", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}

func (h MHandler) DelMessageToUser(c *gin.Context) {
	resp := new(models.RespData)
	req := new(docModel.DelMessageToUserReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("User:AddMessageToUser with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("User:AddMessageToUser with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//删除
	if err := h.M.MessageDeleteById(req.MessageID); err != nil {
		zap.L().Error("Message AddMessageToUser h.M.MessageDeleteById", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}

func (h MHandler) UpdateMessageStatus(c *gin.Context) {
	resp := new(models.RespData)
	req := new(docModel.UpdateMessageStatusReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Message:UpdateMessageStatus with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Message:UpdateMessageStatus with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	exist, info, err := h.M.MessageCheckByID(req.MessageId)
	if err != nil {
		zap.L().Error("Message UpdateMessageStatus h.M.MessageCheckByID", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if !exist {
		resp.SendCode(c, models.CodeUserNotExist)
		return
	}
	//更新状态
	info.Status = req.Status
	err = h.M.MessageUpdateById(info)
	if err != nil {
		zap.L().Error("Message UpdateMessageStatus h.M.MessageUpdateById", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}
